﻿<?php
	@session_start();
	require_once("../includes/conn.php");
	require_once("wxhongbao.php");
	require_once("./test/oauth2.php");
	$tools=new tools();
	$uwxcode=$_GET['uwxcode'];
	$sk=intval($_GET['sk']);
	$sh=intval($_GET['sh']);
	$type = isset($_GET['type'])?$_GET['type']:'';
	$id = isset($_GET['id'])?$_GET['id']:'';
	$useropenid = isset($_SESSION['openid'])?$_SESSION['openid']:'';
	//echo $id;exit;
	$userdg = isset($_SESSION['username'])?$_SESSION['username']:'';
	if(!$phone)
	{
		echo "<script>alert('非法进入!!');</script>";exit;
	}
	//echo $_GET['s'];exit;
	$sysconfig=$dbconn->fetch($dbconn->query("select cappid,cappsecret,cdenglucode from ".DBQIAN."sys_config limit 1"));
	if ($type=='dg')
	{
		$exsits_fwm = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm=".$s." and status=0 order by id desc limit 1"));
		//获取用户的openid
		if(isset($_GET['code']) && $_GET['code']!='')
		{
			//echo $_GET['code'];exit;
			if (isset($exsits_fwm)&&$exsits_fwm)
			{
				$sccode = $exsits_fwm['fwm'];
				$WXCODE=$_GET['code'];
				$getucodeurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret']."&code=".$WXCODE."&grant_type=authorization_code";
				$tools=new tools();
				$getucodejson=json_decode($tools->http_curl_get($getucodeurl,true));
				$ucode=$getucodejson->openid;
				$wxlintoken=$getucodejson->access_token;
			   	//测试发放红包
				//   $packet = new Wxapi();
				//   $packet->pay($ucode);
				//   exit;
				//   var_dump($getucodejson);
			  	// echo "发红包".$WXCODE."<br>";
				$arr['openid']=$ucode;//"o-OMMt0_5vpsTuKKPboU_0OW-QWQ"
				$arr['hbname']="卡玫尔";//.$exsits_fwm['credit']."元红包";//公司名称
				$arr['body']="购买的产品为卡玫尔正品,欢迎下次购买！";//红包祝福语
				$arr['fwm']=$sccode;
				$arr['fee']=$exsits_fwm['credit'];
				
				$comm = new Common_util_pub();          
				$re = $comm->sendhongbaoto($arr);
				//print_r($re);
				//exit;
				if ($re['return_code'] = "SUCCESS" && $re['result_code'] = "SUCCESS")
				{
					$utime=date("Y-m-d H:i:s",time());
					$daogou = mysql_fetch_array(mysql_query("select * from ".DBQIAN."daogou where wx_openid='".trim($ucode)."' order by id desc limit 1"));
					$dbconn->fetch($dbconn->query("insert into ".DBQIAN."exchangedg(fwm,credit,time,username,wx_user) values('".$sccode."',".$exsits_fwm['credit'].",'".date('Y-m-d H:s:i',time())."','".$userdg."','".$daogou['wx_user']."')"));//插入红包记录
					$dbconn->fetch($dbconn->query("update ".DBQIAN."fwm set status=1,time='". $utime ."',username='".$userdg."' where fwm='".$arr['fwm']."'"));
					$dbconn->fetch($dbconn->query("insert into ".DBQIAN."user_xianjin(ucode,umoney,xtype,utime)values('".$arr['openid']."',".$arr['fee'].",2,'".$utime."')"));
					$dbconn->fetch($dbconn->query("update ".DBQIAN."user_list set uystate=2 where ucode='".$arr['openid']."'"));
					header("Location:lingguo.php");
				}
				//测试结束
				if($ucode=='')
				{
					header("Location:chaikai.index.php?sk=".$sk."&sh=".$sh."&s=".$s."&type=dg&uwxcode=".$uwxcode);
					exit;
				}
				$urow=$dbconn->fetch($dbconn->query("select uheadimgurl from ".DBQIAN."user_list where ucode='$ucode' order by id desc limit 1"));
			   
			   //下面是授权页面
				if($sysconfig['cdenglucode'] == 1 && $urow['uheadimgurl']=='')
				{
					$locaurl=urlencode(WEBNAME."main/get.info.php?sk=".$sk."&sh=".$sh."&s=".$s."&type=dg&uwxcode=".$uwxcode);
					$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$sysconfig['cappid']."&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_userinfo#wechat_redirect";
					header("Location:".$urls);
					exit;
				}
		   //判断结束
			}
			else 
			{
				 header("Location:lingguo.php");
			}
		} 
		else 
		{
			//echo "你好这是测试信息！";exit;
			header("Location:chaikai.index.php?sk=".$sk."&sh=".$sh."&s=".$s."&type=dg&uwxcode=".$uwxcode);
			exit;
		}
	}
	else //会员
	{
		//会员使用积分领取红包，不需要判断$s的存在以及真假
		if ($type=='user')
		{
			$tixian = mysql_fetch_array(mysql_query("select * from ".DBQIAN."tixian where id=".$id." and status=0 order by time desc limit 1"));
			//获取用户的openid
			if(isset($_GET['code']) && $_GET['code']!='')
			{
				//echo $_GET['code'];exit;
				if (isset($tixian)&&$tixian)
				{
					$openid = $tixian['openid'];
					$WXCODE=$_GET['code'];
					$getucodeurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret']."&code=".$WXCODE."&grant_type=authorization_code";
					$tools=new tools();
					$getucodejson=json_decode($tools->http_curl_get($getucodeurl,true));
					$ucode=$getucodejson->openid;
					$wxlintoken=$getucodejson->access_token;
					$arr['openid']=$ucode;//"o-OMMt0_5vpsTuKKPboU_0OW-QWQ"
					$arr['hbname']="卡玫尔";//.$exsits_fwm['credit']."元红包";//公司名称
					$arr['body']="购买的产品为卡玫尔正品,欢迎下次购买！";//红包祝福语
					$arr['user']=$openid;
					$arr['fee']=$tixian['amount'];
					
					$comm = new Common_util_pub();          
					$re = $comm->sendhongbaoto($arr);
					//print_r($re);
					//exit;
					if ($re['return_code'] = "SUCCESS" && $re['result_code'] = "SUCCESS")
					{
						//发放成功，修改状态，记录信息
						$utime=date("Y-m-d H:i:s",time());
						$dbconn->fetch($dbconn->query("update ".DBQIAN."tixian set status=1,send_time='". $utime ."' where id=".$id));
						$dbconn->fetch($dbconn->query("insert into ".DBQIAN."user_xianjin(ucode,umoney,xtype,utime)values('".$arr['openid']."',".$arr['fee'].",2,'".$utime."')"));
						$dbconn->fetch($dbconn->query("update ".DBQIAN."user_list set uystate=2 where ucode='".$arr['openid']."'"));
						header("Location:lingguo.php");
					}
					else 
					{
						//发放失败，删除对应记录
						$dbconn->fetch($dbconn->query("update ".DBQIAN."users set credit=credit+".$tixian['amount']." where openid='".$openid."'"));
						$dbconn->fetch($dbconn->query("delete from ".DBQIAN."tixian where id=".$id));
					}
					//测试结束
					if($ucode=='')
					{
						header("Location:chaikai.index.php?sk=".$sk."&sh=".$sh."&type=user&uwxcode=".$uwxcode."&id=".$id);
						exit;
					}
					$urow=$dbconn->fetch($dbconn->query("select uheadimgurl from ".DBQIAN."user_list where ucode='$ucode' order by id desc limit 1"));
				   
				   //下面是授权页面
					if($sysconfig['cdenglucode'] == 1 && $urow['uheadimgurl']=='')
					{
						$locaurl=urlencode(WEBNAME."main/get.info.php?sk=".$sk."&sh=".$sh."&type=user&uwxcode=".$uwxcode."&id=".$id);
						$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$sysconfig['cappid']."&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_userinfo#wechat_redirect";
						header("Location:".$urls);
						exit;
					}
			   //判断结束
				}
				else 
				{
					 header("Location:lingguo.php");
				}
			}
			else 
			{
				//echo "你好这是测试信息！";exit;
				header("Location:chaikai.index.php?sk=".$sk."&sh=".$sh."&type=user&uwxcode=".$uwxcode."&id=".$id);
				exit;
			}
		}
	}
		
?>